package com.eslserver.dao;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.to.Coordinator;
import com.eslserver.to.wrapper.ESLCollectionPageWrapper;
import com.eslserver.util.HibernateUtil;

public class DBCoordinatorDAO implements CoordinatorDAO {

	public static final Logger LOG = LoggerFactory
			.getLogger(DBCoordinatorDAO.class);

	protected DBCoordinatorDAO() throws SQLException {
		// Empty protected constructor to avoid unnecessary initialization
	}

	@Override
	public Coordinator addCoordinator(Coordinator eslCoordinator) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.save(eslCoordinator);
		return eslCoordinator;
	}

	@Override
	public void updateCoordinator(Coordinator eslCoordinator) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.update(eslCoordinator);
	}

	@Override
	public Coordinator loadCoordinatorByID(Integer coordinatorID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Coordinator coor = (Coordinator) session.load(Coordinator.class,
				coordinatorID);
		return coor;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Coordinator> loadAllCoordinators() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Coordinator coor = Coordinator.createESLCoordinator();
		coor.setDelete(false);

		Example example = Example.create(coor);

		Collection<Coordinator> coorCollection = session
				.createCriteria(Coordinator.class).add(example).list();

		return coorCollection;
	}

	@Override
	public void deleteCoordinator(Integer coordinatorID) {
		Coordinator eslCoordinator = loadCoordinatorByID(coordinatorID);
		if (eslCoordinator != null) {
			eslCoordinator.setDelete(true);
			updateCoordinator(eslCoordinator);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public ESLCollectionPageWrapper<Coordinator> loadCoordinatorByPageQuery(
			String sQuery, String aQuery) {
		ESLCollectionPageWrapper<Coordinator> coorCollectionPageWrapper = new ESLCollectionPageWrapper<Coordinator>();

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Collection<Coordinator> coorCollection = session.createSQLQuery(sQuery)
				.addEntity(Coordinator.class).list();

		String foundQuery = aQuery;
		BigInteger foundCount = (BigInteger) session.createSQLQuery(foundQuery)
				.uniqueResult();
		coorCollectionPageWrapper.setFilteredTotal(foundCount.intValue());

		String allQuery = "SELECT COUNT(*) FROM esl_coordinator WHERE isDelete=false";
		BigInteger allCount = (BigInteger) session.createSQLQuery(allQuery)
				.uniqueResult();
		coorCollectionPageWrapper.setTotal(allCount.intValue());

		coorCollectionPageWrapper.getESLCollection().addAll(coorCollection);

		return coorCollectionPageWrapper;
	}

	@Override
	public Coordinator loadCoordinatorByName(String coordinatorName) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Coordinator coordinator = Coordinator.createESLCoordinator();
		coordinator.setName(coordinatorName);
		Example example = Example.create(coordinator);
		Coordinator coor = (Coordinator) session
				.createCriteria(Coordinator.class).add(example)
				.addOrder(Order.desc("coordinatorID")).uniqueResult();

		return coor;
	}

}
